<?php
// import manager
Magnum::import('utility/stack/manager');
Magnum::import('head/css');
Magnum::import('head/js');
/**
 * head Controller
 * @uses Magnum_View
 * @uses Magnum_Utility_Stack
 */
class Magnum_Head_Manager extends Magnum_Utility_Stack_Manager
{
    /**
     * the view obj
     * @var Magnum_View
     */
    protected $_view = null;


    /**
     * css...
     * @var Magnum_Head_Css
     */
    protected $_css = null;


    /**
     * Js...
     * @var Magnum_Head_Js
     */
    protected $_js = null;


    /**
     * initialise
     * @param Magnum_View $view the view object
     * @return void
     */
    public function __construct(Magnum_View $view)
    {
        // use view to fit in the stack for this...
        $this->_log(__function__, 'initialize head control');

        $this->_view = $view;
        // init new css singleton
        $this->_css = new Magnum_Head_Css;
        // init new js singleton
        $this->_js = new Magnum_Head_Js;

        // set initial meta to utf-8
        $this->setMeta('Content-Type', 'text/html; charset=utf-8');

        $this->_log(__function__, 'all objects for head control online!');

    }// end Magnum_View


    // get js/css object
    public function js()
    {
        return $this->_js;
    }// end js


    public function css()
    {
        return $this->_css;
    }// end css
    /*-----------------------------[meta/head control]-----------------------------*/

    /**
     * set title
     * @param str $title the title
     * @param str $type  the type
     * @return self
     */
    public function setTitle($title, $type = 'APPEND')
    {
        $this->set('title', $title, $type);
        return $this;

    }// end setTitle


    /**
     * get title
     * @param bool $print if true, print
     * @return void
     */
    public function getTitle($print = true)
    {
        $title = '<title>' . $this->get('title') . '</title>' . EOL;
        if ($print) {
            echo $title;
            return;
        }
        return $title;

    }// end getTitle


    /**
     * set meta
     * @param str $name the name
     * @param str $content the value
     * @return self
     */
    public function setMeta($name, $content)
    {
        $this->get('meta', true)->set('<meta name="' . $name . '" content="' . $content . '">', 'M_' . $name);
        return $this->_log(__function__, func_get_args());

    }// end setMeta


    /**
     * set HttpEquiv
     * @param str $name the name
     * @param str $content the value
     * @return self
     */
    public function setHttpEquiv($name, $content)
    {
        $this->get('meta', true)->set('<meta http-equiv="' . $name . '" content="' . $content . '">', 'H_' . $name);
        return $this->_log(__function__, func_get_args());

    }// end setHttpEquiv


    /**
     * get meta in one go
     * @param bool $print if true, print
     * @return void
     */
    public function getMeta($print = true)
    {
        return $this->get('meta', 1)->export($print);

    }// end getMeta


    /**
     * get css
     * @param bool $print if true, print
     * @return void
     */
    public function getCss($print = true)
    {
        return $this->_css->export($print);

    }// end getCss


    /**
     * add css file or source...
     * @param str $src   source/file
     * @param str $media type of media
     * @return self
     */
    public function setCss($src, $media = 'screen')
    {
        $this->_css->set($src, $media);
        return $this->_log(__function__, func_get_args());

    }// end setCss


    /**
     * add css file or source...
     * @param str $src   source/file
     * @param str $media type of media
     * @return self
     */
    public function appendCss($src, $media = 'screen')
    {
        $this->_css->append($src, $media);
        return $this->_log(__function__, func_get_args());

    }// end appendCss


    /**
     * add css file or source...
     * @param str $src   source/file
     * @param str $media type of media
     * @return self
     */
    public function prependCss($src, $media = 'screen')
    {
        $this->_css->prepend($src, $media);
        return $this->_log(__function__, func_get_args());

    }// end prependCss


    /**
     * export to flat html
     * @param bool $print if true, print
     * @return mixed
     */
    public function export($print = true)
    {
        // get title...
        $tmp = $this->getTitle(0);
        // get meta
        $tmp .= $this->getMeta(0);
        // get css
        $tmp .= $this->getCss(0);
        // get Js
        $tmp .= $this->getJs(0);

        // export!
        if ($print) {
            echo $tmp;
        }

        return $tmp;

    }// end export


    /*-----------------------------[js]--------------------------*/

    /**
     * get Js
     * @param bool $print if true, print
     * @return void
     */
    public function getJs($print = true)
    {
        return $this->_js->export($print);

    }// end getJs


    /**
     * add Js file or source...
     * @param str $src   source/file
     * @return self
     */
    public function setJs($src)
    {
        $this->_js->set($src);
        return $this->_log(__function__, func_get_args());

    }// end setJs


    /**
     * add Js file or source...
     * @param str $src   source/file
     * @return self
     */
    public function appendJs($src)
    {
        $this->_js->append($src);
        return $this->_log(__function__, func_get_args());

    }// end appendJs


    /**
     * add Js file or source...
     * @param str $src   source/file
     * @return self
     */
    public function prependJs($src)
    {
        $this->_js->prepend($src);
        return $this->_log(__function__, func_get_args());

    }// end prependJs

    /*-----------------------------[utils]--------------------------*/

    /**
     * very dangerous function
     * @param enum $type takes JS|CSS|META|TITLE
     * @param str $key the key to remove
     * @return $this
     */
    public function remove($type = null, $key = null)
    {
        $this->_log(__function__, func_get_args());
        switch($type) {
            case 'JS':
                $this->_js->remove($key);
                break;
            case 'CSS':
                $this->_css->remove($key);
                break;
            case 'META':
                $this->get('meta', true)->remove($key);
                break;
            default:
                // remove all!!!
                $this->_js->remove($key);
                $this->_css->remove($key);
                $this->get('meta', true)->remove($key);
                break;
        }

        return $this;

    }// end remove


    // debug
    public function _dump()
    {
        return $this->_stack->_dump();

    }// end dump


    /**
     * log source
     * @param str $message, the message
     * @param str $func the function name
     * @param mixed $params the params, can be anything
     * @param bool $isErr if true, it's an error!
     */
    protected function _log($func, $params = null, $isErr = false)
    {
        if (!defined('_LOG') || !_LOG) {
            return $this;
        }
        // construct the log from here...
        $details = array(
            'class' => __class__,
            'func' => $func,
            'params' => $params
        );

        if (is_array($params)) {
            $params = $params[0];
        }

        __log($func . ': <small>' . $params . '</small>', 'head', $details, $isErr);
        return $this;

    }// end _log

}// end Magnum_Head_Manager
?>